package bTree.LeetCode;

import java.util.HashMap;


public class 猜数字游戏 {
    public String getHint(String secret, String guess) {
        HashMap<Character, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < secret.length(); i++) {
            char c = secret.charAt(i);
            if (hashMap.containsKey(c)) {
                hashMap.put(c, hashMap.get(c) + 1);
            } else {
                hashMap.put(c, 1);
            }
        }
        int A = 0, B = 0;
        for (int i = 0; i < guess.length(); i++) {
            char c1 = guess.charAt(i);
            char c2 = secret.charAt(i);
            if (c1 == c2) {
                A++;
                hashMap.put(c1, hashMap.get(c1) - 1);
            }
        }
        for (int i = 0; i < guess.length(); i++) {
            char c1 = guess.charAt(i);
            char c2 = secret.charAt(i);
            if (c1 != c2) {
                if (hashMap.containsKey(c1) && hashMap.get(c1) != 0) {
                    hashMap.put(c1, hashMap.get(c1) - 1);
                    B++;
                }
            }


        }
        return A + "A" + B + "B";
    }
}